package keeto.com.vn.classified.widgets;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.database.BuiltinService;
import keeto.com.vn.classified.database.BuiltinServiceManager;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.ui.HomeClassifeldActivity;
import keeto.com.vn.classified.ui.MainApplication;
import keeto.com.vn.classified.ui.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

public class LogoutDialog extends Dialog implements
		android.view.View.OnClickListener {
	private Context ctx;
	private Button btnOk;
	private Button btnCancel;
	private Listenner mListener = new Listenner();
	private LinearLayout llParent;
	private BuiltinService mBuiltinService=null;
	private BuiltinServiceManager mBuiltinServiceManager=null;
	public LogoutDialog(Context context) {
		super(context);
		this.ctx = context; 
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.logout_dialog);
		llParent = (LinearLayout) findViewById(R.id.parent_logout_dialog);
		btnOk = (Button) findViewById(R.id.btn_ok_logout_classified);
		btnCancel = (Button) findViewById(R.id.btn_cancel_logout_classified);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		this.setCanceledOnTouchOutside(false);
		Rect displayRectangle = new Rect();
		Window window = this.getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
		llParent.setMinimumWidth((int) (displayRectangle.width() * 0.9f));
		mBuiltinService= BuiltinServiceManager.getInstance(ctx).getBuitinService();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ok_logout_classified:
			String authenKey=MainApplication.getAuthenKey();
			ClassifiedCore.getInstance(ctx).taskLogout( mListener, MainApplication.getAuthenKey());
			this.dismiss();
			break;
		case R.id.btn_cancel_logout_classified:
			this.dismiss();
			break;

		default:
			break;
		}
		
	}

	class Listenner extends ClassifiedListener {
		@Override
		public void finishLogoff(final int returnCode) {
			// TODO Auto-generated method stub
			super.finishLogoff(returnCode);
			((Activity) ctx).runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (returnCode == IProtocol.STATUS_CODE_OK_200) {
						// preferences.clear();
					} else if (returnCode == IProtocol.STATUS_CODE_GENERICFAIL_500) {
						// Toast.makeText(ctx,
						// ctx.getResources().getString(R.string.e),
						// Toast.LENGTH_SHORT).show();
					} else {
						// Toast.makeText(ctx,
						// ctx.getResources().getString(R.string.error_logout),
						// Toast.LENGTH_SHORT).show();
					}
					String name = mBuiltinService.getUserName();
					BuiltinServiceManager.getInstance(ctx).deleteBuitinService(name);
					String name1 = mBuiltinService.getUserName();
					Log.e("TAGGG", name1+" .aaa");
					HomeClassifeldActivity.actionLaunch(ctx);
					((Activity) ctx).finish();
					LogoutDialog.this.dismiss();
				}
			});

		}
	}

}
